package com.baozun.service.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baozun.mapper.TFriendMapper;
import com.baozun.model.TFriend;
import com.baozun.model.TFriendExample;
import com.baozun.service.FriendService;
import com.baozun.util.PageData;

/**
 * 
 * @Title:
 * @Company:宝尊电子商务有限公司
 * @Description:
 * @Author:xiaozhou.zhou
 * @Since:2015年1月23日
 * @Copyright:Copyright (c) 2014
 * @ModifyDate:
 * @Version:1.1.0
 */
@Service("friendService")
@Transactional
public class FriendServiceImpl implements FriendService {
    
    @Autowired
    private TFriendMapper friendMapper;
    
    @Override
    public void add(TFriend bean) {
        if (StringUtils.isEmpty(bean.getStFriendId())) {
            bean.setStFriendId(UUID.randomUUID().toString());
        }
        bean.setDtCreateTime(new Date());
        friendMapper.insert(bean);
    }
    
    @Override
    public TFriend get(String id) {
        return friendMapper.selectByPrimaryKey(id);
    }
    
    @Override
    public void update(TFriend bean) {
        bean.setDtUpdateTime(new Date());
        friendMapper.updateByPrimaryKey(bean);
    }
    
    @Override
    public void delete(String stFindMailId) {
        // TODO Auto-generated method stub
        friendMapper.deleteByPrimaryKey(stFindMailId);
    }
    
    @Override
    public PageData<TFriend> getPageListByExample(TFriendExample record, int pageSize, int currentPage) {
        record.setSize(pageSize);
        record.setStart(pageSize * (currentPage - 1));
        PageData<TFriend> infoList = new PageData<TFriend>();
        List<TFriend> appendixList = friendMapper.selectByExample(record);
        int count = friendMapper.countByExample(record);
        infoList.addAll(appendixList);
        infoList.setPage(currentPage);
        infoList.setPages((count + pageSize - 1) / pageSize);
        infoList.setPageSize(pageSize);
        infoList.setTotalRecords(count);
        return infoList;
    }
    
    @Override
    public List<TFriend> getList(TFriendExample record) {
        // TODO Auto-generated method stub
        return friendMapper.selectByExample(record);
    }
    
    @Override
    public int getCount(TFriendExample record) {
        // TODO Auto-generated method stub
        return friendMapper.countByExample(record);
    }
    
    @Override
    public TFriend findByFriendId(String stSelfId, String stFriendId) {
        // TODO Auto-generated method stub
        TFriendExample record = new TFriendExample();
        record.createCriteria().andStUserIdRequestEqualTo(stFriendId).andStUserIdResponseEqualTo(stSelfId);
        record.or(record.createCriteria().andStUserIdResponseEqualTo(stFriendId).andStUserIdRequestEqualTo(stSelfId));
        List<TFriend> friendList = getList(record);
        TFriend friend = null;
        if (!CollectionUtils.isEmpty(friendList)) {
            friend = friendList.get(0);
        }
        return friend;
    }
}
